Skip to content

fix(routing): accept a query or hash after a built route URL - #896

Open
0froq wants to merge 1 commit into
voidzero-dev:mainfrom
0froq:fix/route-url-query-suffix
Open

0froq wants to merge 1 commit into
voidzero-dev:mainfrom
0froq:fix/route-url-query-suffix

Conversation

@0froq

@0froq 0froq commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

This description was drafted by an agent from the diff and the commands it ran.

Fixes #893

Description

RouteUrl accepted a query or hash suffix only after a route path, so appending ?page=2 to a routeUrl() result dropped out of RouteUrl. router.visit(), Link, and props typed RouteUrl rejected it, and Void 1.9.1 accepted the same code.

The suffix now applies to a shared base, BuiltRouteUrl | RoutePathUrl, in the runtime type and in the declaration void prepare writes. Both spellings of a route URL take the same suffix, so the union has one place to change.

A suffix on a routeUrl() result that already carries query or hash still type-checks, as in the issue's ${routeUrl('/a', { query: { a: 1 } })}?b=2. BuiltRouteUrl is opaque, so telling that apart needs a second brand. Route path strings already allow the same join.

Test

The typed fixture in route-url-builder.test.ts now appends a query and a hash to a RouteUrl, to a routeUrl() result, and passes both to router.visit(). It also pins that a suffix does not skip the route check (/unknown?page=2) and that it needs its own separator.

With the same fixture on the base types:

check-typed.ts(58,7): error TS2322: Type '... | `${BuiltRouteUrl}?page=2` | ...' is not assignable to type ...
  Type '`${BuiltRouteUrl}?page=2`' is not assignable to type ...
check-typed.ts(68,19): error TS2345: Argument of type '... | `${BuiltRouteUrl}?page=2` | ...' is not assignable to parameter of type 'RouteUrlInput'.

`RouteUrl` allowed a suffix only after a route path, so adding `?page=2` to
a `routeUrl()` result failed the type check. Apply the suffix to a shared
base instead, in both the runtime and the generated declaration.
@yangshun

yangshun commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the fix and the thorough tests. After looking into it, I don't think we should take do this. Here's why:

It type-checks URLs that are broken at runtime. BuiltRouteUrl is opaque, so the type can't tell whether routeUrl() already added a query or hash. With this change, both of these compile:

`${routeUrl('/a', { query: { a: 1 } })}?b=2`; // /a?a=1?b=2
`${routeUrl('/a', { hash: 'top' })}?b=2`; // /a#top?b=2, so the query ends up in the hash

Today the compiler rejects both.

It encourages building queries by hand. Appending a query string by hand skips encoding. We already have structured ways to add a query, and they encode values:

  • routeUrl(path, { query, hash })
  • { pathname, params, query } destinations for router.visit() and redirect()
  • Link's data prop, which merges into an existing query and keeps the hash

It doesn't hold up in large apps. `${url}?page=2` builds a union as large as RouteUrl and compares it member by member. With a synthetic route union, TypeScript 7.0.2 reports TS2859 ("Excessive complexity comparing types") from about 700 routes, and 6.0.3 somewhere between 1,000 and 1,400 routes. Those apps would still need a cast, and on TS 7 the error is harder to read than today's TS2345. The test fixture is too small to show this.

It only helps when the URL is passed inline. const next = `${routeUrl('/about')}?page=2` still widens to string, so router.visit(next) keeps failing.

It adds to the public types. In the generated declare module "void/routes" block, RouteUrlBase becomes importable from void/routes even without export, and we'd then need to keep it for compatibility.

It also overlaps with #861 (I'm still working on), which reworks these same declarations.

I know this pattern compiled in 1.9.1, and the issue is legit, but this way is probably not recommended for reasons mentioned above.

The routes stuff is admittedly a bit messy at the moment (thanks to me). I'm intending to take a step back and rethink how we should do routes in a cleaner fashion but haven't had time to do so.

@0froq

0froq commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator Author

@yangshun Thank you! That's fair. The scale point is the one I hadn't checked at all. I also didn't realise a type declared inside the generated declare module "void/routes" block becomes importable, so RouteUrlBase would have been a second public name to keep around. I'll close this.

Still wondering about the docs though. "Checked URL strings" says a typed string must match a route "optionally followed by a query string or hash", and the example right below passes a routeUrl() result next to plain strings. Reads like ${routeUrl('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/admin/users')}?page=2 is fine, and in 1.9.1 it was. Nothing user-facing says that stopped, or why.

Would a short paragraph there be welcome, or does #861 change what the docs should say and I should wait for it? Was thinking it covers which forms take a suffix, that a routeUrl() result goes in as-is with query/hash passed at build time and Link's data for links, and why an appended suffix is rejected (the type can't tell whether the URL already has a query or hash, and the join skips encoding). One bit I'm unsure how to word: if you already hold a RouteUrl and want to add a query, the only typed route left is a destination object, which wants pathname and params again. Is "rebuild with routeUrl(path, { query })" what you'd document there, or does that belong to the rework too?

If you'd rather keep these files quiet until #861 lands, happy to sit on it. Just wanted to offer :)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A routeUrl() result with a query appended isn't a RouteUrl

2 participants